CREATE PROCEDURE GalleryGroup_FindAll
AS
BEGIN
    Select 
    GroupId,
    ApplicationId,
    Name
    from GalleryGroup
END
GO

CREATE PROCEDURE GalleryGroup_FindById
@GroupId int
AS
BEGIN
    Select * from GalleryGroup where GroupId = @GroupId
END
GO

CREATE PROCEDURE GalleryGroup_FindByName
@name nvarchar(50)
AS
BEGIN
    Select * from GalleryGroup where GroupId = @name
END
GO

CREATE PROCEDURE GalleryGroup_Insert
        @GroupId int ,
        @ApplicationId int ,
        @Name nvarchar (50)
AS
BEGIN
INSERT INTO GalleryGroup
(
        GroupId,
        ApplicationId,
        Name
)
VALUES
(
        @GroupId,
        @ApplicationId,
        @Name
)
select @GroupId = @@IDENTITY
END
GO

CREATE PROCEDURE GalleryGroup_Update
        @GroupId int ,
        @ApplicationId int ,
        @Name nvarchar (50),
AS
BEGIN
UPDATE  GalleryGroup SET
    ApplicationId = @ApplicationId,
    Name = @Name
    WHERE GroupId = @GroupId
END
GO

CREATE PROCEDURE GalleryGroup_Delete
    @GroupId int
AS
BEGIN
    DELETE FROM GalleryGroup where GroupId = @GroupId
END
GO

CREATE PROCEDURE GalleryGroup_Paging
    @PageSize int, 
    @PageNumber int
    AS 
    declare @RowStart int
    declare @RowEnd int
    if @PageNumber > 0 
    BEGIN	
    set @PageNumber = @PageNumber - 1
    set @RowStart = @PageSize * @PageNumber + 1;
    set @RowEnd = @RowStart  + @PageSize - 1;
    SELECT TotalRowCount = COUNT(GroupId) FROM GalleryGroup;
    WITH MyTable as 
    (
    SELECT * ,  ROW_NUMBER() OVER (order by SortIndex) as RowNumber From  GalleryGroup
    )
    SELECT * from MyTable where RowNumber >= @RowStart and RowNumber <= @RowEnd
END
GO

CREATE proc GalleryGroup_SetActive
@GroupId int
AS
declare @Type bit
SELECT @Type =  IsActive from GalleryGroup where GroupId = @GroupId
BEGIN
IF(@Type = 1)
Update GalleryGroup set IsActive = 0 where GroupId = @GroupId
else
Update GalleryGroup set IsActive = 1 where GroupId = @GroupId
END
GO

CREATE proc GalleryGroup_SetSortIndex
@GroupId int, 
@Type int
AS
declare @CurrentSort int
select @CurrentSort = SortIndex from  GalleryGroup where GroupId = @GroupId
BEGIN
IF(@Type = 1)
Update GalleryGroup set SortIndex =  @CurrentSort + 1 where GroupId = @GroupId
else
Update GalleryGroup set SortIndex =  @CurrentSort - 1 where GroupId = @GroupId
END
GO

CREATE PROCEDURE GalleryGroup_RemoveSelectedItem
    @GroupId int
AS
BEGIN
    UPDATE GalleryGroup SET IsDeleted = 1 WHERE GroupId = @GroupId
END
GO

CREATE PROCEDURE GalleryGroup_RestoreSelectedItem
    @GroupId int
AS
BEGIN
    UPDATE GalleryGroup SET IsDeleted = 0 WHERE GroupId = @GroupId
END
GO

CREATE PROCEDURE GalleryGroup_RemoveAll
    @GroupId int
AS
BEGIN
    UPDATE GalleryGroup SET IsDeleted = 1
END
GO

CREATE PROCEDURE GalleryGroup_RestoreAll
    @GroupId int
AS
BEGIN
    UPDATE GalleryGroup SET IsDeleted = 0
END
GO

